package org.vectrics.struts.actions;

import org.apache.log4j.*;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.vectrics.common.utils.NetUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;


abstract public class SavePageToFileAction extends Action {
    private static Category log = Category.getInstance(SavePageToFileAction.class); 
	
	abstract protected String getIpAddress(HttpServletRequest request);
	abstract protected String getFileName();
	

	public ActionForward execute(ActionMapping mapping,
				 ActionForm form,
				 HttpServletRequest request,
				 HttpServletResponse response)	throws JspException {
				 	
		String ipAddress = getIpAddress(request);
		
		try { 
			NetUtils.saveHtmlPageToFile(ipAddress, getFileName(), false);
		}
		catch (org.vectrics.VectricsException ve) {
			log.error("VectricsException while acquiring page: " + ipAddress, ve);
			throw new JspException("MalformedURLException while acquiring page: " + ipAddress, ve);
		}
				 	
		return (mapping.findForward("success"));
	}
}
